<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: tracking.php 546 2015-02-28 12:03:10Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 546 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2015-02-28 12:03:10 +0000 (Sat, 28 Feb 2015) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

$ref_url = parse_url(@$_SERVER['HTTP_REFERER']);

//echo_e($_SERVER);
//echo_e($_GET);
//echo_e('.', 'e');

$_script = @$_SERVER['SCRIPT_NAME'];

if (!in_array($_script, array('/style.css.php', '/java.js.php'))) {
	if ($_SESSION['tracked'] != true) {
		$_SESSION['tracking']['http_referer'] = $ref_url;
		$_SESSION['tracked'] = true;
	}

	if (!isset($_SESSION['tracking']['ip'])) 
		$_SESSION['tracking']['ip'] = $_SERVER['REMOTE_ADDR'];

	if (!isset($_SESSION['tracking']['refID']) || $_SESSION['tracking']['refID'] == "") {
		if(isset($_GET['refID']) && $_GET['refID'] != "") {
			$campaign_check = $db->db_query("SELECT
													*
												FROM
													".TABLE_CAMPAIGNS."
																			WHERE
																				campaigns_refID = ".$db->db_prepare($_GET['refID'])."");

			if($campaign_check->_numOfRows == 1) {
				$_SESSION['tracking']['refID'] = $_GET['refID'];
				$insert_sql = array('user_ip' => $_SESSION['tracking']['ip'],
									'campaign' => $_GET['refID'],
									'time' => 'NOW()');
				$check_date = mktime(0, date("i")-1, 0,date("m"), date("d"), date("Y"));
				$ip_query = $db->db_query("	SELECT
												*
											FROM
												".TABLE_CAMPAIGNS_IP."
											WHERE
												campaign = ".$db->db_prepare($_GET['refID'])."
											AND
												user_ip = '".$_SESSION['tracking']['ip']."'
											AND
												time > '".$check_date."'");
				if(!$ip_query->_numOfRows)
					$db->db_perform(TABLE_CAMPAIGNS_IP, $insert_sql);
			}
		}
	}

	if(!isset($_SESSION['tracking']['date']))
		$_SESSION['tracking']['date'] = (date("Y-m-d H:i:s"));

	if(!isset($_SESSION['tracking']['browser']))
		$_SESSION['tracking']['browser'] = $_SERVER["HTTP_USER_AGENT"];

	$i = count($_SESSION['tracking']['pageview_history']);

	if ($i > 6) {
		array_shift($_SESSION['tracking']['pageview_history']);
		$_SESSION['tracking']['pageview_history'][6]['from'] = $ref_url;
		$_SESSION['tracking']['pageview_history'][6]['page'] = @$_SERVER['REDIRECT_URL']; 
		//$_SESSION['tracking']['pageview_history'][6]['script'] = @$_SERVER['SCRIPT_NAME']; 

	} else {
		$_SESSION['tracking']['pageview_history'][$i]['from'] = @$_SERVER['REDIRECT_URL']; $ref_url;
		$_SESSION['tracking']['pageview_history'][$i]['page'] = @$_SERVER['REDIRECT_URL']; 
		//$_SESSION['tracking']['pageview_history'][$i]['script'] = @$_SERVER['SCRIPT_NAME']; 
	}

	//echo_e($_SESSION['tracking']['pageview_history']);

	if(isset($_SESSION['tracking']['pageview_history'][$i]) && $_SESSION['tracking']['pageview_history'][$i] == $_SESSION['tracking']['http_referer'])
		array_shift($_SESSION['tracking']['pageview_history']);
} //!in_array($_script, array('/style.css.php', '/java.js.php'))) {